<template>
    <div class="AIRoom">
        <div class="AIView" ref="scrollContainer">
            <div v-for="item in props.AIMessageList">
        <!-- 其他用户的消息 -->
        <div class="other" v-if="item.id ==='1'">
          <div class="hImg" v-if="item.imageUrl">
            <img style="border-radius: 6px;width: 100%;height: 100%;" :src="item.imageUrl" @error="setDefaultImage($event)">
          </div>
          <div>
            <p style="margin-left: 10px; font-size: smaller;">{{ item.name }}</p>
            <div class="message">{{ item.message }}
              <!-- 添加播放按钮 -->
              <el-button style="width: 20px;height: 20px;margin: 0 0 0 5px;"
              link
               @click="playSpeech(item.message)"
               :icon="VideoPlay"
               class="play-btn"
              />
              <span>{{ item.createTime }}</span>
            </div>
            
            
          </div>
        </div>
        <!-- 当前用户的消息 -->
        <div class="me" v-else>
          <div>
            <p style="font-size: smaller; text-align: right; margin-right: 10px;">{{ item.name }}</p>
            <div class="message">{{ item.message }}</div>
          </div>
          <div class="hImg">
            <img style="border-radius: 6px;width: 100%;height: 100%;" :src="item.imageUrl">
          </div>
        </div>
      </div>
        </div>
        <div class="input">
      <el-input type="textarea" placeholder="请输入..." v-model.trim="AIInput" @keyup.enter="sendAI" :autosize="{ minRows: 6.5, maxRows: 6.5 }" resize="none" style="width: 100%;height: calc(100% - 50px); float: left;"/>
        <el-tooltip
          effect="dark"
          content="发送"
          placement="top"
        >
          <el-button class="input-anniu" @click="sendAI">
            <el-icon><Position /></el-icon>
          </el-button>
        </el-tooltip>
        <!-- <el-tooltip
          effect="dark"
          :content="tooltipContent"
          placement="top"
        >
          <el-button  class="input-anniu">
            <el-icon>
              <component :is="recordingIcon" />
            </el-icon>
          </el-button>
        </el-tooltip> -->
    </div>
    </div>
</template>
<script setup>
  import { ref,defineEmits,defineProps,watch,onUpdated, onMounted } from 'vue';
  import { VideoPlay,Microphone, Mic,Position } from '@element-plus/icons-vue'
  // import Recorder from 'recorder-core';
  // import 'recorder-core/src/engine/mp3';
  // import 'recorder-core/src/engine/mp3-engine';
  // import 'recorder-core/src/engine/wav';
  // import 'recorder-core/src/extensions/waveview';
  import{playSpeech,initSpeech} from '../forum/js/voice'

  const emit = defineEmits(['custom-event']);
  const AIInput=ref("")

const props = defineProps({
  AIMessageList: {
    type: Array,
    required: true
  },
  user: {
    type: Object,
    required: true
  }
});
const sendAI=()=>{
    props.AIMessageList.push({id:props.user.id,message:AIInput.value,imageUrl:props.user.imageUrl,name:props.user.name},)
    console.log(AIInput.value)
    emit('custom-event', AIInput.value,3);
    AIInput.value=""
}

// 获取滚动容器引用
const scrollContainer = ref(null)

// 滚动到底部
const scrollToBottom = () => {
  if (scrollContainer.value) {
    scrollContainer.value.scrollTop = scrollContainer.value.scrollHeight
  }
}
onMounted(()=>{
    scrollToBottom()
    initSpeech()
})
// 更新消息列表后滚动到底部
onUpdated(() => {
  scrollToBottom()
})
</script>
<style scoped>
.AIRoom{
    width: 100%;
    height: 100%;
    /* background-color: yellowgreen; */
}
.AIView{
    overflow: auto;
    width: 100%;
    height: calc(100% - 200px);
    background-color: rgb(227, 228, 227);
    /* border-radius: 20px; */
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}


/* 其他用户的消息样式 */
.other {
  margin: 10px;
  width: calc(100% - 30px);
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
}
/* 头像样式 */
.other .hImg {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  margin-bottom: auto;
}
/* 消息内容样式 */
.other .message {
  position: relative;
  word-wrap: break-word;
  background-color: rgb(255, 255, 255);
  margin: 0 10px;
  border-radius: 6px;
  padding: 8px 30px 8px 12px;
  max-width: 300px;
  span{
    font-size: 13px;
    color: rgb(128, 128, 128);
    width: 200px;
    position: absolute;
    /* background-color: yellowgreen; */
    left: 105%;
    bottom: 0;
  }
}
/* 当前用户的消息样式 */
.me {
  margin: 10px;
  width: calc(100% - 30px);
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}
/* 当前用户头像样式 */
.me .hImg {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  margin-bottom: auto;
}
/* 当前用户消息内容样式 */
.me .message {
  word-wrap: break-word;
  background-color: rgb(28, 199, 42);
  margin: 0 10px;
  border-radius: 6px;
  padding: 8px 12px;
  position: relative;
  max-width: 300px;
  span{
    font-size: 13px;
    color: rgb(128,128,128);
    width: 200px;
    position: absolute;
    right: 80%;
    top: 50%;
  }
}
:deep(.play-btn){
  width: 40px;
  /* background-color: yellowgreen; */
  position: absolute;
  /* float: right; */
  right: 5px;
  bottom: 2px;
  /* align-items: end; */
}

.input{
    width: 100%;
    height: 200px;
    /* background-color: aliceblue; */
}
  
.input-anniu {
    width: 50px;
    height: 50px;
    float: right;
    border-radius: 50%;
    margin-right: 20px;
    margin-bottom: 50px;
    /* padding-bottom: 20px; */
    /* position: absolute; */
    bottom: 0;
    font-size: 24px;
    color: #fff;
    background-color: #d6d5de;
  }
</style>